﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iCollect.Constants;
using System.Globalization;
using iCollect.util;

namespace iCollect.CollectDetail
{
    public partial class Config2 : UserControl
    {
        public Config2()
        {
            InitializeComponent();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBoxPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        //private DataTable dt;

        public void loadDevice()
        {
            try
            {
                string deviceCode = GlobalConstants.currentDevice;
                systemparamTableAdapter.FillBy(cUPDataDataSet.systemparam, deviceCode);
                if (cUPDataDataSet.systemparam.Rows.Count == 0)
                {
                    systemparamTableAdapter.InsertQuery(deviceCode);
                    systemparamTableAdapter.FillBy(cUPDataDataSet.systemparam, deviceCode);
                }
                loadControls();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void loadControls()
        {
            DataRowCollection rows = cUPDataDataSet.systemparam.Rows;
            if (rows.Count != 1)
            {
                return;
            }
            DataRow row = rows[0];

            bool configSimOn = (bool)row["analog"];
            bool configExOn = (bool)row["exon"];
            bool configTE = (bool)row["te"];
            bool configVT = (bool)row["vt"];
            bool configPT = (bool)row["pt"];
            bool configFT = (bool)row["ft"];
            bool configRT = (bool)row["rt"];
            decimal configFreq = Convert.ToDecimal(row["frequency"]);
            int configStoreTime = Convert.ToInt32(row["timelength"]);
            List<DateTime> configTimePoint = getTimeList(row["timepoint"].ToString());
            int configExLength = Convert.ToInt32(row["exlength"]); ;
            int configExMaxCount = Convert.ToInt32(row["exmaxcount"]);
            int configExInterval = Convert.ToInt32(row["exinterval"]);

            setControls(configSimOn, configExOn,
                configTE, configVT, configPT, configFT, configRT,
                configFreq, configStoreTime, configTimePoint,
                configExLength, configExMaxCount, configExInterval);
            setConfig(configSimOn, configExOn,
                configTE, configVT, configPT, configFT, configRT,
                configFreq, configStoreTime, configTimePoint,
                configExLength, configExMaxCount, configExInterval);
        }

        private void resetControls()
        {
            bool configSimOn = true;
            bool configExOn = true;
            bool configTE = true;
            bool configVT = true;
            bool configPT = true;
            bool configFT = true;
            bool configRT = true;
            decimal configFreq = 1;
            int configStoreTime = 5;
            List<DateTime> configTimePoint = new List<DateTime>();
            int configExLength = 50;
            int configExMaxCount = 10;
            int configExInterval = 10;

            setControls(configSimOn, configExOn,
                configTE, configVT, configPT, configFT, configRT, 
                configFreq, configStoreTime, configTimePoint,
                configExLength, configExMaxCount, configExInterval);
            setConfig(configSimOn, configExOn,
                configTE, configVT, configPT, configFT, configRT,
                configFreq, configStoreTime, configTimePoint,
                configExLength, configExMaxCount, configExInterval);
        }

        private void setControls(bool configSimOn, bool configExOn,
            bool configTE, bool configVT,  bool configPT, bool configFT,  bool configRT,
            decimal configFreq,  int configStoreTime, List<DateTime> configTimePoint,
            int configExLength, int configExMaxCount, int configExInterval)
        {
            textBoxPath.Text = "";
            SimulationTrue.Checked = configSimOn;
            SimulationFalse.Checked = !configSimOn;
            ExceptionStoreTrue.Checked = configExOn;
            ExceptionStoreFalse.Checked = !configExOn;
            cbTE.Checked = configTE;
            cbVT.Checked = configVT;
            cbPT.Checked = configPT;
            cbFT.Checked = configFT;
            cbRT.Checked = configRT;
            cbFreq.Text = configFreq.ToString();
            cbSymbolStoreTime.Text = configStoreTime.ToString();
            setListBox(listBoxNormalStore, configTimePoint);
            cbExceptionStoreLength.Text = configExLength.ToString();
            cbExceptionStoreOccurCount.Text = configExMaxCount.ToString();
            numExceptionTriggerInterval.Value = configExInterval;
        }

        private void setConfig(bool configSimOn, bool configExOn,
            bool configTE, bool configVT, bool configPT, bool configFT, bool configRT,
            decimal configFreq, int configStoreTime, List<DateTime> configTimePoint,
            int configExLength, int configExMaxCount, int configExInterval)
        {
            GlobalConstants.configSimOn = configSimOn;
            GlobalConstants.configExOn = configExOn;
            GlobalConstants.configTE = configTE;
            GlobalConstants.configVT = configVT;
            GlobalConstants.configPT = configPT;
            GlobalConstants.configFT = configFT;
            GlobalConstants.configRT = configRT;
            GlobalConstants.configFreq = configFreq;
            GlobalConstants.configTimeLength = configStoreTime;
            GlobalConstants.configTimePoint.Clear();
            GlobalConstants.configTimePoint = configTimePoint;
            GlobalConstants.configExLength = configExLength;
            GlobalConstants.configExMaxCount = configExMaxCount;
            GlobalConstants.configExInterval = configExInterval;
        }

        private void btSave_Click(object sender, EventArgs e)
        {

            string listValue = getListBox(listBoxNormalStore);
            decimal freq;
            int int1, int2, int3;
            freq = Convert.ToDecimal(cbFreq.Text);
            int.TryParse(cbSymbolStoreTime.Text, out int1);
            int.TryParse(cbExceptionStoreLength.Text, out int2);
            int.TryParse(cbExceptionStoreOccurCount.Text, out int3);
            this.systemparamTableAdapter.UpdateQuery(textBoxPath.Text, SimulationTrue.Checked, 
                ExceptionStoreTrue.Checked,cbTE.Checked, cbVT.Checked, cbPT.Checked, cbFT.Checked, cbRT.Checked,
                freq, int1, listValue, int2, int3, (int)numExceptionTriggerInterval.Value, GlobalConstants.currentDevice);
            UIUtil.getInstance().alert("保存成功。");
        }

        private string getListBox(ListBox listbox)
        {
            string listValue = "";
            for (int i = 0; i < listbox.Items.Count; i++)
            {
                TimePoint tp = (TimePoint)listbox.Items[i];
                listValue += tp.data.Ticks + ";";
            }
            if (listbox.Items.Count > 0)
            {
                listValue = listValue.Remove(listValue.Length - 1);
            }
            return listValue;
        }

        private void setListBox(ListBox listbox, List<DateTime> listValue)
        {
            listbox.Items.Clear();
            foreach (DateTime dt in listValue)
            {
                listbox.Items.Add(new TimePoint(dt));
            }
        }

        private List<DateTime> getTimeList(string listValue)
        {
            List<DateTime> dtList = new List<DateTime>();
            if (listValue == null || listValue.Equals(""))
            {
                return dtList;
            }
            string[] values = listValue.Split(new char[] { ';' });
            DateTime dt;
            foreach (string value in values)
            {
                long time = long.Parse(value);
                dt = new DateTime(time);
                dtList.Add(dt);
            }
            return dtList;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DateTime time = dateTimePickerNormalStore.Value;
            TimePoint tp = new TimePoint(time);
            listBoxNormalStore.Items.Add(tp);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            int index = listBoxNormalStore.SelectedIndex;
            if(index == -1)
            {
                return;
            }
            listBoxNormalStore.Items.RemoveAt(index);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.loadControls();
        }


    }
}
